﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;

namespace ResultsManagementSystem
{
    class TeacherDao
    {
        private Teacher teacher;
        private SqlCeConnection conn;
        private SqlCeCommand cmd;
        private SqlCeDataReader rdr;

        public TeacherDao()
        {
            this.teacher = new Teacher();

            conn = new SqlCeConnection(@"Data Source=../../Results.sdf");
            conn.Open();

            cmd = conn.CreateCommand();
        }

        ~TeacherDao()
        {
            conn.Close();
        }

        public Teacher getTeacher()
        {
            return this.teacher;
        }

        public void setTeacher(Teacher teacher)
        {
            this.teacher = teacher;
        }

        public Teacher querryTeacherByName(String name)
        {
            cmd.CommandText = "SELECT * FROM Teacher where name = '" + name + "'";
            
            rdr = cmd.ExecuteReader();

            if (rdr.Read())
            {
                this.teacher.setId(rdr.GetInt32(0));
                this.teacher.setName(rdr.GetString(1));
                this.teacher.setPassword(rdr.GetString(2));
            }
            else
            {
                this.teacher = null;
            }

            return this.teacher;

        }
    }
}
